package com.share.dgs.fetcher;

import com.netflix.graphql.dgs.DgsComponent;
import com.netflix.graphql.dgs.DgsData;
import com.netflix.graphql.dgs.InputArgument;
import com.share.dgs.enums.MovieType;
import com.share.dgs.model.ActionMovie;
import com.share.dgs.model.Actor;
import com.share.dgs.model.Movie;
import com.share.dgs.model.ScaryMovie;
import graphql.schema.DataFetchingEnvironment;

import java.util.ArrayList;
import java.util.List;

/**
 * @author caifan
 * @created 2021/3/5
 * description:
 */
@DgsComponent
public class MovieDataFetcher {
    @DgsData(parentType = "Query", field = "movies")
    public List<Movie> movies(DataFetchingEnvironment dfe) {
        return initMovie(MovieType.All);
    }

    /**
     * 根据枚举类型不同获取不同类型的结果，graphql中使用的是内联片段
     * @param filter
     * @return
     */
    @DgsData(parentType = "Query", field = "movieFilter")
    public List<Movie> movieFilter(@InputArgument("filter")MovieType filter) {
        return initMovie(filter);
    }

    @SuppressWarnings("SameReturnValue")
    @DgsData(parentType = "Movie", field = "director")
    public String director() {
        return "some director";
    }

    private List<Movie> initMovie(MovieType movieType) {
        List<Movie> movies = new ArrayList<>();
        Actor actor1 = new Actor();
        actor1.setHome("广州");
        actor1.setName("张三");
        Actor actor2 = new Actor();
        actor2.setHome("上海");
        actor2.setName("李四");
        if (movieType.equals(MovieType.Action) || movieType.equals(MovieType.All)) {
            movies.add(new ActionMovie("Crouching Tiger", null, 0, actor1));
            movies.add(new ActionMovie("Black hawk down", null, 10, actor1));
        }
        if (movieType.equals(MovieType.Scary) || movieType.equals(MovieType.All)) {
            movies.add(new ScaryMovie("American Horror Story", null, true, 10, actor2));
            movies.add(new ScaryMovie("Love Death + Robots", null, false, 4, actor2));
        }
        return movies;
    }
}
